package cn.datastruct.test;



class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    @Override
    public String toString() {
        return "ListNode{" +
                "val=" + val +
                '}';
    }
}

public class Palindrome {
    public ListNode ll;

    //构造函数
    public Palindrome(int a) {
        this.ll = new ListNode(a);
    }

    //到末尾添加链表节点
    public void addNode(ListNode ln) {
        //添加节点需要借助辅助节点找到最后一个节点
        ListNode temp = this.ll;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = ln;
    }

    public boolean isPalindrome(ListNode head) {
        //求链表长度
        int length = 0;
        ListNode p;
        ListNode temp = head;
        ListNode heada = new ListNode();
        while (temp != null) {
            if (length == 0) {
                p = temp;
                heada = p;
                length++;
            } else {
                p = temp.next;
                temp.next = heada;
                heada = temp;
            }
            temp = p;
        }
        //判断
        boolean flag = false;
        ListNode temp1 = head;
        ListNode temp2 = heada;

        while (temp1 != null) {
//            if (temp1.val != temp2.val) {
//                flag = true;
//                break;
//            }
            temp1 = temp1.next;
            temp2 = temp2.next;
            System.out.println(temp1);
            System.out.println(temp2);
        }
        return flag;

    }
    public void showLinkedNode() {
        ListNode temp = this.ll.next;
        if (temp == null)
            System.out.println("链表为空");
        else {
            while (temp != null) {
                System.out.println(temp);
                temp = temp.next;
            }
        }
    }
}
